package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.RoleBean;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

public class AddRole implements ServerInterface{

	private static Logger log = LogManager.getLogger(AddRole.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<String> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", String.class);
		try {
			RoleBean role = JsonUtils.objectToPojo(req.getData(), RoleBean.class);
			if(StringUtils.isEmpty(role.getRoleName())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("角色不能为空");
				return msg;
			}
			role.setApp(ApplicationConfig.getAppCustomerAppid());
			ServiceFactory.getRoleService().addRole(role);
		} catch (Exception e) {
			log.error("角色添加出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("角色添加出错");
		}
		return msg;
	}

}
